home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_TOOL / TOOLS / TOOLS_WI / BYACC__ / PRINT.C < prev    next >
C/C++ Source or Header  |  1989-11-19  |  4KB  |  227 lines

  1. #include <stdio.h>
  2. #include "action.h"
  3. #include "defs.h"
  4. #include "dep.h"
  5. #include "new.h"
  6. #include "files.h"
  7. #include "gram.h"
  8. #include "state.h"
  9.  
  10. verbose()
  11. {
  12.   register int i;
  13.  
  14.   putc(FF,verbose_file);
  15.   putc(NEWLINE, verbose_file);
  16.   if (SRtotal + RRtotal > 0)
  17.     log_conflicts();
  18.  
  19.   for (i = 0; i < nstates; i++)
  20.     print_state(i);
  21. }
  22.  
  23.  
  24. log_conflicts()
  25. {
  26.   register int i;
  27.  
  28.   for (i = 0; i < nstates; i++)
  29.     {
  30.       if (SRconflicts[i] || RRconflicts[i])
  31.     {
  32.       fprintf(verbose_file, "State %3d contains ", i);
  33.       if (SRconflicts[i] == 1)
  34.         fprintf(verbose_file, "1 shift/reduce conflict");
  35.       else if (SRconflicts[i] > 1)
  36.         fprintf(verbose_file, "%d shift/reduce conflicts", SRconflicts[i]);
  37.       if (SRconflicts[i] && RRconflicts[i])
  38.         fprintf(verbose_file, " and ");
  39.       if (RRconflicts[i] == 1)
  40.         fprintf(verbose_file, "1 reduce/reduce conflict");
  41.       else if (RRconflicts[i] > 1)
  42.         fprintf(verbose_file, "%d reduce/reduce conflicts", RRconflicts[i]);
  43.       fprintf(verbose_file, ".\n");
  44.     }
  45.     }
  46.   fprintf(verbose_file, "\n\n");
  47. }
  48.  
  49.  
  50. print_state(state)
  51. int state;
  52. {
  53.   fprintf(verbose_file, "\n\nstate %3d\n", state);
  54.   if (state < final_state)
  55.     {
  56.       print_core(state);
  57.       print_actions(state);
  58.     }
  59.   else
  60.     fprintf(verbose_file, "\n    accept\n");
  61. }
  62.  
  63.  
  64. print_core(state)
  65. int state;
  66. {
  67.   register int i;
  68.   register int k;
  69.   register int rule;
  70.   register core *statep;
  71.   register short *sp;
  72.   register short *sp1;
  73.  
  74.   statep = state_table[state];
  75.   k = statep->nitems;
  76.   if (k == 0) return;
  77.  
  78.   putc('\n', verbose_file);
  79.   for (i = 0; i < k; i++)
  80.     {
  81.       sp1 = sp = ritem + statep->items[i];
  82.  
  83.       while (*sp > 0)
  84.     sp++;
  85.  
  86.       rule = -(*sp);
  87.       fprintf(verbose_file, "    %s  ->  ", symbol_name[rlhs[rule]]);
  88.  
  89.       for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
  90.     {
  91.       fprintf(verbose_file, "%s ", symbol_name[*sp]);
  92.     }
  93.  
  94.       putc('.', verbose_file);
  95.  
  96.       while (*sp > 0)
  97.     {
  98.       fprintf(verbose_file, " %s", symbol_name[*sp]);
  99.       sp++;
  100.     }
  101.  
  102.       putc('\n', verbose_file);
  103.     }
  104. }
  105.  
  106.  
  107. print_actions(stateno)
  108. int stateno;
  109. {
  110.   register action *p;
  111.   register shifts *sp;
  112.   register int i, as;
  113.  
  114.   p = parser[stateno];
  115.   if (p)
  116.     {
  117.       print_shifts(p);
  118.       print_reductions(p, defred[stateno]);
  119.     }
  120.  
  121.   sp = shift_table[stateno];
  122.   if (sp && sp->nshifts > 0)
  123.     {
  124.       as = accessing_symbol[sp->shifts[sp->nshifts - 1]];
  125.       if (ISVAR(as))
  126.     print_gotos(stateno);
  127.     }
  128. }
  129.  
  130.  
  131. print_shifts(p)
  132. register action *p;
  133. {
  134.   register int count;
  135.   register action *q;
  136.  
  137.   count = 0;
  138.   for (q = p; q; q = q->next)
  139.     {
  140.       if (q->suppressed < 2 && q->action_code == SHIFT)
  141.     count++;
  142.     }
  143.  
  144.   if (count > 0)
  145.     {
  146.       putc(NEWLINE, verbose_file);
  147.       for (; p; p = p->next)
  148.     {
  149.       if (p->action_code == SHIFT)
  150.         {
  151.           if (p->suppressed == 0)
  152.         {
  153.           fprintf(verbose_file, "    shift  %3d  %s\n", p->number,
  154.               symbol_name[p->symbol]);
  155.         }
  156.           else if (p->suppressed == 1)
  157.         {
  158.           fprintf(verbose_file, "   <shift  %3d  %s>\n", p->number,
  159.               symbol_name[p->symbol]);
  160.         }
  161.         }
  162.     }
  163.     }
  164. }
  165.  
  166.  
  167. print_reductions(p, defred)
  168. register action *p;
  169. register int defred;
  170. {
  171.   register int k, anyreds;
  172.   register action *q;
  173.  
  174.   anyreds = 0;
  175.   for (q = p; q ; q = q->next)
  176.     {
  177.       if (q->action_code == REDUCE && q->suppressed < 2)
  178.     {
  179.       anyreds = 1;
  180.       break;
  181.     }
  182.     }
  183.  
  184.   if (anyreds)
  185.     {
  186.       putc(NEWLINE, verbose_file);
  187.       for (; p; p = p->next)
  188.     {
  189.       if (p->action_code == REDUCE && p->number != defred)
  190.         {
  191.           k = p->number;
  192.           if (p->suppressed == 1)
  193.         fprintf(verbose_file, "   <reduce %3d  %s>\n",
  194.             k, symbol_name[p->symbol]);
  195.           else if (p->suppressed == 0)
  196.         fprintf(verbose_file, "    reduce %3d  %s\n",
  197.             k, symbol_name[p->symbol]);
  198.         }
  199.     }
  200.  
  201.       if (defred >= 0)
  202.     fprintf(verbose_file, "    reduce %3d  $default\n", defred);
  203.     }
  204. }
  205.  
  206. print_gotos(stateno)
  207. int stateno;
  208. {
  209.   register int i, k;
  210.   register int as;
  211.   register short *to_state;
  212.   register shifts *sp;
  213.  
  214.   putc(NEWLINE, verbose_file);
  215.   sp = shift_table[stateno];
  216.   to_state = sp->shifts;
  217.   for (i = sp->nshifts - 1; i >= 0; i--)
  218.     {
  219.       k = to_state[i];
  220.       as = accessing_symbol[k];
  221.       if (ISVAR(as))
  222.     fprintf(verbose_file, "    goto   %3d  %s\n", k, symbol_name[as]);
  223.       else
  224.     break;
  225.     }
  226. }
  227.